package pr.iceworld.fernando.api.oms.gateway.util;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import pr.iceworld.fernando.entity.User;

import java.util.Objects;

public class SessionUtils {

    public final static String CURRENT_USER = "CURRENT_USER";

    public final static String REMEMBER_ME = "REMEMBER_ME";

    public static Session getCurrentSession() {
        Subject subject = SecurityUtils.getSubject();
        return Objects.isNull(subject) ? null : subject.getSession();
    }

    public static String getCurrentSessionId() {
        return Objects.isNull(getCurrentSession()) ? null : getCurrentSession().getId().toString();
    }

    public static Long getCurrentUserId() {
        return getCurrentUser() == null ? null : (Long) getCurrentUser().getId();
    }

    public static User getCurrentUser() {
        return getCurrentSession() == null ? null : (User) getCurrentSession().getAttribute(CURRENT_USER);
    }

    public static void setAttribute(String key, Object value) {
        Session session = getCurrentSession();
        if (session != null) {
            session.setAttribute(key, value);
        }
    }
}
